<template>
	<a-modal
		:visible="visible"
		class="invoicing-info-moadal"
		width="750px"
		title="销方开票详情"
		centered
		destroyOnClose
		:maskClosable="false"
		@cancel="cancel">
		<a-descriptions size="small">
			<a-descriptions-item label="开票信息状态">{{ invoicingInfo[!props.detail.disabled ? 1 : 2].name }}</a-descriptions-item>
		</a-descriptions>
		<a-descriptions size="small" :column="2">
			<a-descriptions-item label="适用门店">{{ info.departmentName }}</a-descriptions-item>
			<a-descriptions-item label="不开票费用科目">
				{{ (info.disabledFeeSubjectIds || []).map(o => o.name).join('、') || '无科目' }}
			</a-descriptions-item>
		</a-descriptions>
		<template v-for="(item, index) in info.orgItemList" :key="index">
			<a-typography-text strong>
				<blockquote style="margin-top: 10px">{{ item.isDefault ? '默认收款账号' : '特殊费用配置' }}</blockquote>
			</a-typography-text>
			<!-- <a-descriptions size="small" :column="3"></a-descriptions> -->
			<a-descriptions size="small" :column="2">
				<a-descriptions-item :span="2" label="配置费用科目" v-if="!item.isDefault">
					{{ (item.feeSubjectIds || []).map(o => o.name).join('、') || '无科目' }}
				</a-descriptions-item>
				<a-descriptions-item label="企业名称">{{ enterpriseInfo(item.invOrgId) }}</a-descriptions-item>
				<a-descriptions-item label="税号">
					{{ modalConfirmData.enterpriseInfo[index]?.taxpayerNum }}
				</a-descriptions-item>
				<!-- <a-descriptions-item label="税盘号+终端号">{{ item.machineCode + '+' + item.extensionNum }}</a-descriptions-item> -->
				<!-- <a-descriptions-item label="剩余发票数量">
					{{ modalConfirmData.enterpriseInfo[index]?.invoiceSurplusNum }}
				</a-descriptions-item>
				<a-descriptions-item label="单张开票限额">
					{{
						getInvoiceMaxValue(modalConfirmData.enterpriseInfo[index]?.machineInfos || [], item.machineCode) ||
						modalConfirmData.enterpriseInfo[index]?.invoiceValue
					}}
				</a-descriptions-item> -->
				<a-descriptions-item label="开票设备类型">
					{{
						(item.deviceType || []).map(taxControlDeviceType => equipmentType[taxControlDeviceType]?.name).join('、') ||
						'暂无信息'
					}}
				</a-descriptions-item>
				<a-descriptions-item label="开票人姓名">{{ item.drawerName }}</a-descriptions-item>
				<a-descriptions-item label="复核人姓名">{{ item.reviewerName }}</a-descriptions-item>
				<a-descriptions-item label="收款人姓名">{{ item.takerName }}</a-descriptions-item>
				<a-descriptions-item label="地址">{{ item.sellerAddress }}</a-descriptions-item>
				<a-descriptions-item label="电话">{{ item.sellerTel }}</a-descriptions-item>
				<a-descriptions-item label="开户行">{{ item.sellerBankName }}</a-descriptions-item>
				<a-descriptions-item label="银行账号">{{ item.sellerBankAccount }}</a-descriptions-item>
			</a-descriptions>
			<a-typography-text v-if="show(item.deviceType)" strong>
				<blockquote style="margin-top: 10px">金税盘/税控盘/税务 UKEY配置</blockquote>
			</a-typography-text>
			<a-descriptions v-if="show(item.deviceType)" size="small" :column="2">
				<a-descriptions-item label="税盘号+终端号">{{ item.machineCode + '+' + item.extensionNum }}</a-descriptions-item>
				<a-descriptions-item label="剩余发票数量">
					{{ modalConfirmData.enterpriseInfo[index]?.invoiceSurplusNum }}
				</a-descriptions-item>
				<a-descriptions-item label="单张开票限额">
					{{
						getInvoiceMaxValue(modalConfirmData.enterpriseInfo[index]?.machineInfos || [], item.machineCode) ||
						modalConfirmData.enterpriseInfo[index]?.invoiceValue
					}}
				</a-descriptions-item>
			</a-descriptions>
			<a-typography-text v-if="item.deviceType?.includes(5)" strong>
				<blockquote style="margin-top: 10px">全电发票配置</blockquote>
			</a-typography-text>
			<a-descriptions v-if="item.deviceType?.includes(5)" size="small" :column="1">
				<a-descriptions-item label="主体是否存在不动产租赁服务相关的税收分类编码">
					{{ item?.existImmovables ? '是' : '否' }}
				</a-descriptions-item>
			</a-descriptions>
			<a-descriptions v-if="item.deviceType?.includes(5)" size="small" :column="2">
				<a-descriptions-item label="不动产所在地">{{ item?.region || '-' }}</a-descriptions-item>
				<a-descriptions-item label="不动产详细地址">{{ item?.detailedAddress || '-' }}</a-descriptions-item>
				<a-descriptions-item label="面积单位">{{ item?.areaUnit || '-' }}</a-descriptions-item>
				<a-descriptions-item label="是否跨地(市)">
					<div v-if="!item.existImmovables">-</div>
					<div v-else>
						{{ item.crossCitySign ? '是' : '否' }}
					</div>
				</a-descriptions-item>
				<a-descriptions-item label="租赁起止日">
					{{ item?.leaseBeginDate || '-' }}至{{ item?.leaseEndDate || '-' }}
				</a-descriptions-item>
				<a-descriptions-item label="产权证书/不动产权号">{{ item?.titleNo || '-' }}</a-descriptions-item>
			</a-descriptions>
		</template>

		<template #footer>
			<a-button v-if="props.detail.disabled && hasPermissions(['mk_finance_invoicing_information_sc'])" @click="handle('delete')">
				删除
			</a-button>
			<a-button
				type="primary"
				v-if="props.detail.disabled && hasPermissions(['mk_finance_invoicing_information_bj'])"
				@click="handle('update')">
				编辑
			</a-button>
			<a-button
				type="primary"
				v-if="props.detail.disabled && hasPermissions(['mk_finance_invoicing_information_qy'])"
				@click="handle('enable')">
				启用
			</a-button>
			<a-button
				type="primary"
				v-if="!props.detail.disabled && hasPermissions(['mk_finance_invoicing_information_jy'])"
				@click="handle('disabled')">
				禁用
			</a-button>
		</template>
	</a-modal>
	<!-- @success="getList" -->
	<secondary-confirmation v-model:visible="visibles.certification" :data="modalConfirmData" @success="cancel" />
	<!-- 新增/编辑 -->
	<add-invoicing-info v-model:visible="visibles.addReserveVisible" :detail="props.detail" @success="cancel" />
</template>
<script setup lang="ts">
	import {reactive, ref, watch} from 'vue'
	import {message} from 'ant-design-vue'
	import {invoicingInfo} from '@/utils/invoice-administration'
	import {equipmentType} from '@/utils/invoice-administration'
	import {
		departmentConfigDetails,
		updateDisableStatus,
		departmentConfigRemove,
		orgInfoId,
		spiExtensionInfo,
		invoiceMaxValue,
		orgInfoAvailableList
	} from '@/api/invoice-administration'
	import SecondaryConfirmation from '@/components/finance/invoice-administration/secondary-confirmation/secondary-confirmation.vue'
	import {useStore} from '@/store/index'

	const hasPermissions = useStore().hasPermissions

	const props = defineProps({
		detail: {
			type: Object,
			required: true
		},
		visible: {
			type: Boolean,
			required: true
		}
	})
	const visibles = reactive<any>({
		certification: false,
		addReserveVisible: false
	})
	//弹框提示
	const modalConfirmData = reactive<any>({
		title: '',
		content: '',
		requestMethodFun: null,
		seveDate: {},
		enterpriseInfo: []
	})
	const info = ref<any>({})
	const loading = ref<boolean>(false)
	const disabled = ref(false)
	const emit = defineEmits(['update:visible', 'success'])
	const cancel = () => {
		emit('update:visible', false)
		emit('success')
	}
	const submit = () => {}
	const handle = (field: string) => {
		switch (field) {
			case 'disabled':
				//禁用
				visibles.certification = true
				modalConfirmData.title = '禁用'
				modalConfirmData.content = '禁用之后将无法使用该开票信息进行开票、冲红，确定要禁用该开票信息吗？'
				modalConfirmData.seveDate = {
					departmentId: props.detail.departmentId,
					disabled: true
				}
				modalConfirmData.requestMethodFun = updateDisableStatus
				console.log('禁用')
				break
			case 'enable':
				//启用、
				visibles.certification = true
				modalConfirmData.title = '启用'
				modalConfirmData.content = '启用之后将可以使用该开票信息进行开票、冲红，确定要启用该开票信息吗？'
				modalConfirmData.seveDate = {
					departmentId: props.detail.departmentId,
					disabled: false
				}
				modalConfirmData.requestMethodFun = updateDisableStatus
				console.log('启用')
				break
			case 'delete':
				//删除
				visibles.certification = true
				modalConfirmData.title = '删除'
				modalConfirmData.content = '删除之后将无法使用该开票信息进行开票、冲红，确定要删除该开票信息吗？'
				modalConfirmData.seveDate = {
					departmentId: props.detail.departmentId
				}
				modalConfirmData.requestMethodFun = departmentConfigRemove
				console.log('删除')
				break
			case 'update':
				//编辑
				visibles.addReserveVisible = true
				modalConfirmData.seveDate = {
					departmentId: props.detail.departmentId
				}
				console.log('编辑')
				break
		}
	}
	const getDepartmentConfigDetails = () => {
		departmentConfigDetails(props.detail.departmentId).then(res => {
			info.value = res.data
			// for (let item of res.data.orgItemList) {
			// 	getEnterpriseInfo(item.invOrgId)
			// }
			for (let i in res.data.orgItemList) {
				getEnterpriseInfo(res.data.orgItemList[i].invOrgId, i)
			}
		})
	}
	const getEnterpriseInfo = async (id: string, index: number) => {
		orgInfoId(id).then(res => {
			//票通暂停
			modalConfirmData.enterpriseInfo.push({
				taxpayerNum: res.data.taxpayerNum,
				invoiceValue: res.data.invoiceValue,
				remainInvoices: res.data?.invoiceSurplusNum || null,
				extensionNum: res.data.extensionNum,
				machineInfos: res.data?.machineInfos || []
			})
			res.data.sourceType === 1 && getSpiExtensionInfo(id, index)
		})
	}
	//获取税盘号，剩余发票数量
	const getSpiExtensionInfo = (id: string, index: number) => {
		try {
			spiExtensionInfo(id).then(res => {
				modalConfirmData.enterpriseInfo[index].invoiceSurplusNum = res.data[0]?.invoiceSurplusNum || null
				let info = {
					extensionNum: res.data[0].extensionNum,
					machineCode: res.data[0].machineCode,
					orgId: id
				}
				getInvoiceValue(info, index)
			})
		} catch {}
	}
	//获取发票限额
	const getInvoiceValue = (info: object, type: number) => {
		invoiceMaxValue(info).then(res => {
			modalConfirmData.enterpriseInfo[type].invoiceValue = res.data
		})
	}
	//获取最大发票限额
	const getInvoiceMaxValue = (list, machineCode) => {
		return list.filter(o => o.machineCode === machineCode)[0]?.invoiceMaxValue
	}
	const funInfo = async (taxpayerNum: string, id: string) => {
		// const extensionInfo: any = await spiExtensionInfo(id)
		// const invoiceValue: any = await invoiceMaxValue({orgId: id})
		// modalConfirmData.enterpriseInfo.push({
		// 	taxpayerNum: taxpayerNum,
		// 	invoiceValue: extensionInfo.data.invoiceValue,
		// 	remainInvoices: extensionInfo.data.invoiceSurplusNum
		// })
	}
	//企业列表
	const enterpriseList = ref<any[]>([])
	// 查询可用企业
	const getEnterpriseList = () => {
		let obj = {
			size: 100,
			status: 3
		}
		orgInfoAvailableList(obj).then(res => {
			enterpriseList.value = [...res.data]
			// (data: any[]) => (data.value = d)
		})
	}
	//查询企业名称
	const enterpriseInfo = id => {
		return enterpriseList.value.filter(o => o.id === id)[0].name || ''
	}
	watch(
		() => props.visible,
		(newVal, oldVal) => {
			if (props.detail.departmentId && newVal) {
				getEnterpriseList()
				getDepartmentConfigDetails()
			}
		}
	)
	//是否展示
	const show = (data: any) => {
		if (data?.includes(1) || data?.includes(2)) {
			return true
		}
		return false
	}
</script>
<style lang="less">
	.invoicing-info-moadal {
		.url-input {
			position: absolute;
			width: 125px;
			height: 128px;
			z-index: -1;
			top: 0;
			left: 0;
		}
		.text {
			font-size: 12px;
			color: #999;
		}
		.ant-form-vertical .display-flex {
			flex-direction: initial;
			.ant-form-item-control-input-content {
				margin: -8px 0 0 10px;
			}
		}
		.tip-info {
			width: 100%;
			height: 40px;
			line-height: 40px;
			background: #fffbe6;
			border-radius: 2px 2px 2px 2px;
			opacity: 1;
			border: 1px solid #ffe58f;
			font-size: 14px;
			margin-bottom: 20px;
			padding-left: 16px;
		}
		.file-upload-container {
			width: 133px;
			height: 133px;
		}
		.download-code-box {
			width: 140px;
			margin: 35px auto 0;
		}
		.download-code-text {
			text-align: center;
			display: block;
			margin-top: 15px;
		}
	}
</style>
